package com.controller;

import com.bean.District;
import com.bean.Quality;
import com.service.DistrictService;
import com.service.QualityService;
import com.util.PageAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("quality")
public class QualityController {

    @Autowired
    private QualityService qualityService;
    @Autowired
    private DistrictService districtService;

    @RequestMapping("query")
    public String query(HttpSession session, PageAssistant pageAssistant,Integer districtId){

        System.out.println(">>>quality query");
        System.out.println(districtId);

        HashMap<String , Object> map = new HashMap<String ,Object>();
        map.put("start",pageAssistant.getStart());
        map.put("size",pageAssistant.getPageSize());
        map.put("districtId",districtId);

        pageAssistant.setCount(qualityService.count(map));

        List<Quality> qualityList=qualityService.query(map);
        List<District> districtList=districtService.query(map);
        int count =qualityService.count(map);
        System.out.println(count);

        pageAssistant.setItems(qualityList);

        session.setAttribute("districtId",districtId);
        session.setAttribute("districtInfo",districtList);
        session.setAttribute("pageAssistant",pageAssistant);

        if(count<1){
            session.setAttribute("msg","抱歉,暂无数据!!!");
            return "redirect:/quality.jsp";
        }

        return "/quality.jsp";
    }

    //添加
    @RequestMapping("add")
    public String add(HttpSession session,Quality quality){

        System.out.println(">>>quality add");
        System.out.println(quality);

        quality.setLastModifyTime(new Date());
        int count = qualityService.save(quality);

        return "redirect:/quality/query";
    }
}
